#include "ColladaReader.h"

ColladaReader::ColladaReader(){
}
ColladaReader::~ColladaReader(){
}


Data* ColladaReader::read(){

	try {
        XMLPlatformUtils::Initialize();
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Error during initialization! :\n";
        cout << "Exception message is: \n"
                << message << "\n";
        XMLString::release(&message);
    }

	char* xmlFile = "cube.dae";
	SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
    parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
    parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);   // optional

	ColladaHandler* handler = new ColladaHandler();
	parser->setContentHandler(handler);
	parser->setErrorHandler(handler);

	parser->parse(xmlFile);

	try {
        parser->parse(xmlFile);
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Exception message is: \n"
                << message << "\n";
        XMLString::release(&message);
    }
    catch (const SAXParseException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Exception message is: \n"
                << message << "\n";
        XMLString::release(&message);
    }
    catch (...) {
        cout << "Unexpected Exception \n" ;
    }

	std::string vertexes (handler->getVertexesMsg());
	std::string normals (handler->getNormalsMsg());
	std::string polyList (handler->getPolyList());

	float vArray[24];
	float nArray[18];
	int pArray[6*4*2];

	stringstream ss (vertexes);
	for(int i = 0;i < 24;++i){
		ss >> vArray[i];
	}
	ss.~stringstream();
	new ( (void *) &ss ) std::stringstream(normals);
	for(int i = 0;i < 18;++i){
		ss >> nArray[i];
	}
	ss.~stringstream();
	new ( (void *) &ss ) std::stringstream(polyList);
	for(int i = 0;i < 6*4*2;++i){
		ss >> pArray[i];
	}

	float vertexPositions[6*4*3];
	std::vector<float> normalVectors(6*4*3);
	//float normalVectors[6*4*3];
	
	for(int i = 0; i < 6*4; ++i){
		for(int z = 0; z < 3; ++z){
			vertexPositions[(i*3)+z] = vArray[pArray[2*i]*3+z];
			normalVectors[(i*3)+z] = nArray[pArray[2*i+1]*3+z];
		}
	}

	std::vector<float> finalVertices(6*2*3*3);
	//float finalVertices[6 * 2 * 3 * 3];
	{
		int counter = 0;
		for (int i = 0; i < 6*4*3; i += 12){ // From each QUAD we generate 2 triangles
			finalVertices[ counter++ ] = vertexPositions[ i ];
			finalVertices[ counter++ ] = vertexPositions[ i + 1 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 2 ];

			finalVertices[ counter++ ] = vertexPositions[ i + 6 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 7 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 8 ];

			finalVertices[ counter++ ] = vertexPositions[ i + 3 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 4 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 5 ];

			finalVertices[ counter++ ] = vertexPositions[ i + 3 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 4 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 5 ];

			finalVertices[ counter++ ] = vertexPositions[ i + 6 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 7 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 8 ];

			finalVertices[ counter++ ] = vertexPositions[ i + 9 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 10 ];
			finalVertices[ counter++ ] = vertexPositions[ i + 11 ];
		}
	}

	//Data* d = new Data(vertexPositions,normalVectors);
	Data* d = new Data(finalVertices,normalVectors);
	
	
    delete parser;
    delete handler;

	return d;

}


